<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Auth;
use App\User;

class SearchController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    /**
    * 搜索页
    */
    public function index(Request $request) {
        //搜索
        $keywords = $request->input('keywords');
        
        //导航 获取分类
        $cates = $this -> getCatesByDiGui(0);

        //今日推荐
        $today = DB::table('goods')->orderBy('id','desc')->paginate(3);

        //优惠活动
        $huodong = DB::table('goods')->where('hot','=','1')->orderBy('id','desc')->paginate(4);

        //获取商品
        $goods=DB::table('goods')
        ->where(function($query)use($keywords) {
            //如果存在关键字参数
            if(!empty($keywords)) {
                $query->where('spname','like','%'.$keywords.'%');
            } else {
                return "木有找到";
            }
        })
        ->paginate(10);

        if(Auth::check()){
            $id = Auth::user()->id;
            $info = DB::table('userdetail')->where('id','=',$id)->first();
            return view('home.search',['data'=>$request->all(),'goods'=>$goods,'cates'=>$cates,'info'=>$info]);
        } else {
            return view('home.search',['data'=>$request->all(),'goods'=>$goods,'cates'=>$cates]);
        }

    }

    /**
    * 递归方式来获取分类信息
    */
    public function getCatesByDiGui($pid)
    {
        //获取分类
        $cates = DB::table('cates')->where('pid','=', $pid)->get();
        //遍历分类信息
        $sub_cate = [];
        foreach($cates as $k=>$v) {
            $v->sub_cate = $this->getCatesByDiGui($v->id);
            $sub_cate[] = $v;
        }
        return $sub_cate;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
